package com.bobi.tfca.web.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.enums.MemberStatusEnum;
import com.bobi.tfca.model.vo.MemberVO;
import com.bobi.tfca.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private MemberService memberService;

    /**
     * 登陆验证
     * @param telephone 电话号码
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String telephone) throws UsernameNotFoundException {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getTelephone, telephone)
                .eq(MemberEntity::getMemberStatus, MemberStatusEnum.NORMAL.getCode());

        return new UserDetailsWrapper(memberService.getOne(wrapper));
    }
}